package bdz.projects.atwww.electrolux.model.core
{
	import bdz.projects.atwww.electrolux.events.ELEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public dynamic class ELPrograms extends Array implements IEventDispatcher
	{
		private static var dispatcher:EventDispatcher;
		private static var instance:ELPrograms;
		
		public function ELPrograms(se:SingletonEnforcer) {
		
			super();
		}
		public static function getInstance():ELPrograms{
			if(instance == null){
				instance = new ELPrograms(new SingletonEnforcer());
				dispatcher = new EventDispatcher(instance);
			}
			return instance;
		}
		public function parsePrograms(xm:XMLList):void{
			var counter:Number = 0;
			while(length){
				splice(0,1);
			}
			for each(var px:XML in xm){
				push(new ELProgram(px));
			}
		}
		public function replaceProgramWithAlias(xm:XML, alias:String):void{
			
			var counter:Number = 0;
			for each(var ep:ELProgram in instance){
				if(ep.alias.toLowerCase() == alias.toLowerCase()){
					var nep:ELProgram = new ELProgram(xm);
					instance.splice(counter, 1, nep);
					
					dispatchEvent(new ELEvent(ELEvent.PROGRAM_CHANGED, {value:instance[counter], index:counter})); 
					break;
				}
				counter++;
			}
			
		}
		
		/**
		 * EVENT DISPATCHER IMPLEMENTATION
		 */
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
			dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
			dispatcher.removeEventListener(type, listener, useCapture);
		}		
		public function dispatchEvent(event:Event):Boolean {
			return dispatcher.dispatchEvent(event);
		}
		
		public function hasEventListener(type:String):Boolean{
			return dispatcher.hasEventListener(type);
		}		
		public function willTrigger(type:String):Boolean {
			return dispatcher.willTrigger(type);
		}
		/**
		 * END EVENT DISPATCHER IMPLEMENTATION
		 */
		
	}
}
internal class SingletonEnforcer {}